package benny_driving_ser.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Calendar;


import benny_driving_ser.dao.DriPayRentDao;
import benny_driving_ser.util.DBUtil;
import benny_driving_ser.util.FormatUtil;

public class DriPayRentDaoImpl implements DriPayRentDao {

	
	/**
	 * 按照自然月计算
	 */
	public int driPayRent(int driId) {
		
		DecimalFormat df=new DecimalFormat(".##");
		int i = 0;
		
		/*
		 * 是否已经扣款
		 */
		String sql_isPay = "SELECT ispay FROM t_user where id = "+driId;
		/*
		 * 查询当前用户余额
		 */
		String sql_balance = "SELECT ye from t_user WHERE id= "+driId;
		/*
		 * 如果ispay==null 讲ispay改为1
		 */
		String sql_updataIsPayNull = "update t_user set ispay=1 where id="+driId;
		/*
		 * 更改扣款状态
		 */
		String sql_update = "UPDATE t_user SET ispay = 0 where id= "+driId;
		/*
		 * 扣款
		 */
		String sql_deduct = "UPDATE t_user SET ye = ? where id= "+driId;
		/*
		 * 生成缴费订单
		 */
		String sql_createRentOrder = "INSERT into t_pay(je,xtsj,project,driid) VALUES(?,?,?,?)";
		
		
		long submitTime = System.currentTimeMillis();
		String date = FormatUtil.getTimeFormat(submitTime);
		
		long targetTime = get25Millis(submitTime);
		
		
		long deadline =targetTime+(5*24*60*60*1000);
		String  deadlineStr = FormatUtil.getTimeFormat(deadline);
		
		System.out.println("当前系统日期:"+FormatUtil.getTimeFormat(submitTime));
		System.out.println("开始交租时间:"+FormatUtil.getTimeFormat(targetTime));
		System.out.println("交租截止时间"+deadlineStr);
		Connection con = DBUtil.getInstance().openConnection();
		if(submitTime>deadline || submitTime<targetTime){
			//缴费日期不合法
			i = 1;
			return i;
		}
//		{
			try {
				con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
				con.setAutoCommit(false);
				PreparedStatement pst = con.prepareStatement(sql_isPay);
				ResultSet rs = pst.executeQuery();
				while(rs.next()){
					String ispay = rs.getString("ispay");
					if(Integer.parseInt(ispay)==0){
						//本月已扣款
						i = 3;
						return i;
					} else {
						PreparedStatement pstBalance = con.prepareStatement(sql_balance);
						ResultSet rsBalance = pstBalance.executeQuery();
						while(rsBalance.next()){
							if(((int) Double.parseDouble(rsBalance.getString("ye")))<200){
								i = 2;
								return i;
							}else{
								double d = Double.parseDouble(rsBalance.getString("ye"))-200.00;
								BigDecimal bg = new BigDecimal(d);
								double balance = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								System.out.println("余额:"+balance);
								PreparedStatement pstDeduct = con.prepareStatement(sql_deduct);
								pstDeduct.setDouble(1, balance);
								pstDeduct.execute();
							}
						}
					}
				}
				//更改扣款状态
				PreparedStatement pstUpdate = con.prepareStatement(sql_update);
				pstUpdate.executeUpdate();
				//生成扣款订单
				PreparedStatement pstCreateRentOrder = con.prepareStatement(sql_createRentOrder);
				pstCreateRentOrder.setString(1,"-200" );
				pstCreateRentOrder.setString(2,date);
				pstCreateRentOrder.setInt(3,2);
				pstCreateRentOrder.setInt(4,driId);
				pstCreateRentOrder.executeUpdate();
				
				con.commit();
				DBUtil.getInstance().closeConnection(con);
				
			} catch (SQLException e) {
				e.printStackTrace();
				i = 4;
				return i;
			}
//		}
		
		System.out.println("i++++:"+i);
		return i;
	}
	
	
	/**
	 * 获取本月二十五号的毫秒数
	 * @param args
	 */
	public long get25Millis(long submitTime){
	
		Calendar c = Calendar.getInstance();
		
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		
		Calendar c1 = Calendar.getInstance();
		c1.set(Calendar.YEAR,year);
		c1.set(Calendar.MONTH,month-1);
		c1.set(Calendar.DAY_OF_MONTH,25);
		c1.set(Calendar.HOUR_OF_DAY, 0);
		c1.set(Calendar.MINUTE, 0);
		c1.set(Calendar.SECOND, 0);
		long targetTime = c1.getTimeInMillis();
		return targetTime;
	}

	/**
	 * 按照200元/30天计算
	 */
	public int driRentPay(int driId) {
		String payTime = "";
		String rentDueTime = "";
		Connection con = DBUtil.getInstance().openConnection();
		//通过司机Id查询最近缴费时间,到期日期
		String sql_query = "select payTime,rentDueTime from t_user where id = "+driId;
		//更新最近缴费时间
		String sql_update_payTime = "update t_user set payTime= ? where id ="+driId;
		//更新月租到期日期
		String sql_update_rentDueTime = "update t_user rentDueTime = ? where id = "+driId;
		//1.获取司机月租到期时间,(0司机当前剩余时间<40)
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			con.setAutoCommit(false);
			PreparedStatement pst = con.prepareStatement(sql_query);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				payTime = rs.getString("payTime");
				rentDueTime = rs.getString("rentDueTime");
//				if(){}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		long rentDueTime_long = Long.parseLong(rentDueTime);
		//if(){}
		
		//2.司机充值,将司机月租到期时间增加到30天,成功以后将账户余额减去200,同步生成司机扣费记录
		
		//3.做一个线程,司机月租每逐天递减.
		
		
		return 0;
	}
	
	/**
	 * 根据当前毫秒数来判断司机月租还剩余多少天
	 * @param str 当前毫秒数
	 * @return
	 */
	public static int long2int(String str){
		
		long timeMillisOf30days = (long)30*24*60*60*1000;
		long l = Long.parseLong(str)+timeMillisOf30days;
		System.out.println(FormatUtil.getTimeFormat(l));
		
		
		return 0;
	}
	
	
	public static void main(String[] args) {
		
		String s = System.currentTimeMillis()+"";
		
		long2int(s);
	}
}
